package com.tieunguyen.truyencuoi;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ProgressBar;

public class DetailFragment extends Fragment{
	
	int storyId;
	String title;
	String content;
	WebView webview;
	ProgressBar progressBar;
	ProgressDialog progressDialog;
	Handler mHandler = new Handler();
	String html;
	ViewGroup viewGroup;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		storyId = getArguments().getInt("storyId");
		title = getArguments().getString("title");
		content = getArguments().getString("content");
		html = getHTML(storyId, title, content);
	}

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		//return super.onCreateView(inflater, container, savedInstanceState);
		if (viewGroup == null){
			viewGroup = (ViewGroup)inflater.inflate(R.layout.detail_fragment, container, false);
			webview = (WebView)viewGroup.findViewById(R.id.webview);
			progressBar = (ProgressBar)viewGroup.findViewById(R.id.progress_bar);
			mHandler.postDelayed(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					progressBar.setVisibility(View.GONE);
				}
			}, 200);
			webview.loadDataWithBaseURL("", html, "text/html", "utf-8", null);
		}else{
			((ViewGroup) viewGroup.getParent()).removeView(viewGroup);
		}
		return viewGroup;
	}
	
	
	public String getHTML(int storyId, String title, String content){

		String html = "<html><head><meta name=\"viewport\" content=\"width=device-width,initial-scale=1,user-scalable=no\" />"
					+ "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />"
					+ "<style type=\"text/css\" rel=\"stylesheet\">"
					+ "body {font-family: arial, helvetica, sans-serif;}"
					+ "h2 {font-size: 1.5em;}"
					+ "	.detail{text-align: justify; margin: 0; color: #555; line-height: 1.5; font-size: medium; background: #fff;}"
					+ "	.title{font-size: 12px;color: #5C5B5B;padding-left: 0px;}"
					+ "</style>"
					+ "<head><body>"
					+ "<div class=\"content\"><div class=\"title\">"
					+ "<h2>"
					+ title
					+ "</h2>"
					+ "<div class=\"detail\">"
					+ content
					+ "</div>"
					+ "</div>"
					+ "</body></html>";
		return html;
	}
	
}
